
#include "C:\SAFIRE\Y\Y_BIFn\Y_BIFn.y.h"

#include "C:\SAFIRE\Y\PH_SL.y.h"

#include "C:\SAFIRE\Y\Mgt_SL.y.h"

#include "C:\SAFIRE\Y\ConfigUdp_TP.y.h"

#include "C:\SAFIRE\Users\SIP_USER\ws\SIP\source\Common_LIB\Scenario_HTML_Gen.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_LIB\SIP_LIB.y.h"


typedef SIP_ST_CC_TE_CR_V [SIP_Signals_T SIP_G, PH_SL UDP_Data_G, Mgt_SL UDP_Mgt_G]
{
            
    TIMER SIGNAL        TSupervisor();
    TIMER SIGNAL        TAct_Rq();
    TIMER SIGNAL        TResponse();
    TIMER SIGNAL        TStart();
    TIMER SIGNAL        T_Wait_Autoanswer();
    TIMER SIGNAL        T_Msg_Timer();
	TIMER SIGNAL		T_Terminate_Call();
	TIMER SIGNAL		TPostamble();
	
       
    Request_T           Request_TX_1;
   
    Request_Line_T      Request_Line;
    Message_Headers_T   Message_Headers;
	Message_Headers_T	SIP_200_OK_Message_Headers;
    Message_Body_T      Message_Body;
    
    PH_SL               PH_SL_S;
    SIP_Signals_T       SIP_Signal;    
    Config_UDP_T        ConfigUDP;
    
    STRING              CRLF;
        
    STRING              ScenarioName;
    STRING              ScenarioDecription;
    STRING              HTML_Title;
    STRING              HTMLFilePath;
    STRING              Mode;
    STRING              Comment;
	STRING				Tag_Tester;
	STRING				Allow_Tester;
	STRING				Callid_Tester;
	STRING				SIP_Version_Tester;
	STRING				Supported_Tester;
	
    NUMBER              HTML_File;
	NUMBER				Retry_Cancel;
	
	SIP_Addr_Spec_T		SUT_Addr;
	SIP_Addr_Spec_T		Tester_Addr;
	
  
    SUT_Addr.SIP_URI.UserInfo.User = NULL;
	SUT_Addr.SIP_URI.UserInfo.Password = NULL;		
	SUT_Addr.SIP_URI.HostPort.Host = "10.0.1.31";
	SUT_Addr.SIP_URI.HostPort.Host = "10.0.1.159";
	SUT_Addr.SIP_URI.HostPort.Port = 5060;
    SUT_Addr.SIP_URI.URI_Parameters = NULL;
	SUT_Addr.SIP_URI.URI_Headers = NULL;
	
    Tester_Addr.SIP_URI.UserInfo.User = NULL;
	Tester_Addr.SIP_URI.UserInfo.Password = NULL;	
	Tester_Addr.SIP_URI.HostPort.Host = "10.0.1.30";
	Tester_Addr.SIP_URI.HostPort.Host = "10.0.1.109";
	Tester_Addr.SIP_URI.HostPort.Port = 5060;
	Tester_Addr.SIP_URI.URI_Parameters = NULL;
	Tester_Addr.SIP_URI.URI_Headers = NULL;
	
	Tag_Tester 				= "tag=a3bhg";
	SIP_Version_Tester		= "SIP/2.0";
	Allow_Tester 			= "Allow: INVITE, ACK, BYE, CANCEL, REFER, OPTIONS, REGISTER, SUBSCRIBE";
	Callid_Tester			= "6E98FD28E9734BAABC1EE1E5DBA2A7B50x0a00019d";
	Supported_Tester		= "replaces,norefersub,timer";
		
	
	Request_Line.Method.Invite.MustBeNull = NULL;
	Request_Line.Request_URI.Addr_Spec.SIP_URI.HostPort.Host = SUT_Addr.SIP_URI.HostPort.Host;
    Request_Line.SIP_Version = SIP_Version_Tester;
	
    Message_Headers.Call_Id.Callid.Token = Callid_Tester;
    Message_Headers.Contact.Contact_Spec.Contact_Param_List.Contact_Param.Addr.Name_Addr.Display_Name.As_Plain_String.Token_List = SUT_Addr.SIP_URI.UserInfo.User;
	Message_Headers.Contact.Contact_Spec.Contact_Param_List.Contact_Param.Addr.Name_Addr.Addr_Spec.SIP_URI.HostPort.Host = Tester_Addr.SIP_URI.HostPort.Host; 
	Message_Headers.CSeq.Cseq_Number = 1;
	Message_Headers.CSeq.Method.Invite.MustBeNull = NULL;
	Message_Headers.From.From_Spec.Addr.Name_Addr.Display_Name.As_Quoted_String.Quoted_String = "unknown";
    Message_Headers.From.From_Spec.Addr.Name_Addr.Addr_Spec.SIP_URI.HostPort.Host = Tester_Addr.SIP_URI.HostPort.Host;
	Message_Headers.From.From_Spec.From_Param_List.Token = Tag_Tester;        
	Message_Headers.To.To_Spec.Addr.Name_Addr.Addr_Spec.SIP_URI.HostPort.Host = SUT_Addr.SIP_URI.HostPort.Host;
	Message_Headers.Via.Via_Param_List.Via_Param.Sent_Protocol.Token = "SIP/2.0/UDP"; 
	Message_Headers.Via.Via_Param_List.Via_Param.Sent_by.HostPort.Host = Tester_Addr.SIP_URI.HostPort.Host;
	Message_Headers.Via.Via_Param_List.Via_Param.Via_Params_List.Token = "branch=z9hG4bK0a00016d000000184ad450650000109d00000000;rport";	
	Message_Headers.Max_Forwards.Max_Forwards_Number = 70;	
	Message_Headers.Content_Type.Media_Type.Token_List = "application/sdp";
	Message_Headers.Content_Length.ContentLength = 359;
    IF (Supported_Tester EQ "") Message_Headers.Supported =  NULL;
	ELSE Message_Headers.Supported.Option_Tag_List.Token_List =  Supported_Tester;
    Message_Headers.User_Agent.Server_Val_List.Token_List = "SAFIRE 4G - SIP Signaling Tester";
	
	Print("SIP_ST_CC_TE_CR - Message Header is ----------------:");
	Print(Message_Headers);
	
	CRLF = ConvertFromHex("0a0d");   
	Message_Body =   
        "v=0" + CRLF +
        "o=- 3464416997 3464416997 IN IP4 10.0.1.109" + CRLF +
        "s=SJphone" + CRLF +
        "c=IN IP4 10.0.1.109" + CRLF +
        "t=0 0" + CRLF +
        "m=audio 49152 RTP/AVP 3 97 98 8 0 101" + CRLF +
        "c=IN IP4 10.0.1.109" + CRLF +
        "a=rtpmap:3 GSM/8000" + CRLF +
        "a=rtpmap:97 iLBC/8000" + CRLF +
        "a=rtpmap:98 iLBC/8000" + CRLF +
        "a=fmtp:98 mode=20" + CRLF +
        "a=rtpmap:8 PCMA/8000" + CRLF +
        "a=rtpmap:0 PCMU/8000" + CRLF +
        "a=rtpmap:101 telephone-event/8000" + CRLF +
        "a=fmtp:101 0-16" + CRLF +
        "a=setup:active" + CRLF +
        "a=sendrecv" + CRLF;

    
    Request_TX_1.Request_Line = 	Request_Line;    
    Request_TX_1.Message_Headers = 	Message_Headers;             
    Request_TX_1.Message_Body =  	Message_Body;          
        
    
    Mode= "ab";
    HTMLFilePath= "C:\\SAFIRE\\Temp\\SIP_ST_CC_TE_CR_V.html";    
    
    HTML_File= OpenFile( HTMLFilePath, Mode);
    HTML_Title = "SIP_ST_CC_TE_CR_V";
    ScenarioName = "SIP_ST_CC_TE_CR_V";
    ScenarioDecription = "Basic call - active part";
    Scenario_Gen_HTML_Header( HTML_File, HTML_Title, ScenarioName, ScenarioDecription);

    START TStart FOR 1000;
    Print("\n\nScenario_1 : ---  started");
    
    ? .TStart()
    {
         Print("\n\nScenario_1 : ---  started");
         :: Preamble;
    }        
     
    
    STATE Preamble
    {        
        Print("SIP_ST_CC_TE_CR - Start UDP config");
        ConfigUDP.Src.IpAddress=        Tester_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Src.Port=             Tester_Addr.SIP_URI.HostPort.Port;
        ConfigUDP.Dst.IpAddress=        SUT_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Dst.Port=             SUT_Addr.SIP_URI.HostPort.Port;
        ConfigUDP.MaxPacketSize=        1024;
        Print("End UDP config");

        Print("SIP_ST_CC_TE_CR - Configuring UDP");
        UDP_Mgt_G ! Mgt_SL.Configure( ConfigUDP);
        
        Print("SIP_ST_CC_TE_CR - Activating UDP");
        UDP_Data_G ! PH_SL.PH_Act_Rq();
        START TAct_Rq FOR 10000;        
    
        UDP_Data_G ? PH_SL.PH_Act_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_TE_CR - UPD Activated");
            START TSupervisor FOR 10000;
            Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "UDP activated");
            :: Starting;
        }
        
        UDP_Data_G ? PH_SL.PH_Deact_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_TE_CR - UPD Act_Rq not successfull  ... PH_Deact_Ind received!");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "UPD Act_Rq not successfull  ... PH_Deact_Ind received!");            
            STOP;
        }
        
        ? .TAct_Rq()
        {
            Print( "SIP_ST_CC_TE_CR - UPD Act_Req not successfull  ... Timeout!");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "UPD Act_Req not successfull  ... timeout!");
            UDP_Data_G ! PH_SL.PH_Deact_Rq();
            STOP;
        }                
    }             
     
    STATE Starting
    {
        Print("SIP_ST_CC_TE_CR - Starting");
		SIP_G ! SIP_Signals_T.SIP_Invite (Request_TX_1);
        Print("SIP_ST_CC_TE_CR - SIP_Invite message sent ... waiting for SIP_100_Trying");
        START TResponse FOR 5000;        
        :: Wait_Response;
    }

    STATE Wait_Response
    {        

    	SIP_G ? SIP_Signals_T.SIP_100_Trying (?) : SIP_Signal
        {
			Print("SIP_ST_CC_TE_CR - SIP_100_Trying received");
			Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "SIP_100_Trying received"); 
            :: Wait_Response;
        }
    
        SIP_G ? SIP_Signals_T.SIP_180_Ringing (?) : SIP_Signal 
        {
            Print("SIP_ST_CC_TE_CR - VERDICT: PASS! - SIP_180_Ringing received");
            Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "SIP_180_Ringing received"); 
            :: Wait_Response;
                  
        }
        
        SIP_G ? SIP_Signals_T.SIP_200_OK (?) : SIP_Signal 
        {
            Print("SIP_ST_CC_TE_CR - VERDICT: PASS! - SIP_200_OK received");
            Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "SIP_200_OK received");
			
			SIP_200_OK_Message_Headers = SIP_Signal.SIP_200_OK.Response.Message_Headers;
						
			Request_Line.Method.Invite = NULL;
			Request_Line.Method.Ack.MustBeNull = NULL;
			Request_Line.Request_URI.Addr_Spec.SIP_URI.HostPort.Host = SUT_Addr.SIP_URI.HostPort.Host;
			Request_Line.SIP_Version = SIP_Version_Tester;
			
			Request_TX_1.Request_Line = Request_Line;
			
			Request_TX_1.Message_Headers.To.To_Spec.To_Param_List.Token = SIP_200_OK_Message_Headers.To.To_Spec.To_Param_List.Token;
			Request_TX_1.Message_Headers.CSeq.Method = NULL;
			Request_TX_1.Message_Headers.CSeq.Method.Ack.MustBeNull = NULL;
			Request_TX_1.Message_Headers.Content_Type = NULL;
			Request_TX_1.Message_Headers.Supported = NULL;
			Request_TX_1.Message_Headers.Content_Length.ContentLength = 0;
			Request_TX_1.Message_Body = NULL;
			SIP_G ! SIP_Signals_T.SIP_Ack (Request_TX_1);
            START T_Terminate_Call FOR 2000;
			
            :: Terminate_Call;
        
        }

		
		SIP_G ? SIP_Signals_T.SIP_Invalid(?) : SIP_Signal
		{
			Print("SIP_ST_CC_TE_CR - VERDICT: FAIL! - Encoding Error");
            Print(SIP_Signals_T.SIP_Signal.ErrorDescription);
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "Encoding Error");
            START TPostamble FOR 100;
			:: Postamble;
		}
		
		SIP_G ? ? : SIP_Signal 
        {                
            Print("SIP_ST_CC_TE_CR - VERDICT: FAIL! - Unexpected message received");
            Print(SIP_Signal);
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "Unexpected message received");                                 
            START TPostamble FOR 100;
			:: Postamble;
        }
        
     
        ? .TResponse()
        {
            Print("SIP_ST_CC_TE_CR - VERDICT: FAIL! - TResponse timer expiered");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "TResponse timer expiered");                                  
            START TPostamble FOR 100;
			:: Postamble;
        }
        
        ? .TSupervisor()
        {
            Print("SIP_ST_CC_TE_CR - VERDICT: FAIL! - TSupervisor timer expiered");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "TSupervisor timer expiered");
            START TPostamble FOR 100;
			:: Postamble;
        }
    } 
    
    STATE Terminate_Call
    {
        ? .T_Terminate_Call()
        {
            Request_TX_1.Request_Line.Method = NULL;
			Request_TX_1.Request_Line.Method.Bye.MustBeNull = NULL;
			
			Request_TX_1.Message_Headers.CSeq.Method = NULL;
			Request_TX_1.Message_Headers.CSeq.Method.Bye.MustBeNull = NULL;
			Request_TX_1.Message_Headers.CSeq.Cseq_Number = 2;
			IF (Supported_Tester EQ "") Message_Headers.Supported =  NULL;
			ELSE Message_Headers.Supported.Option_Tag_List.Token_List =  Supported_Tester;

			SIP_G ! SIP_Signals_T.SIP_Ack (Request_TX_1);
			
			START TPostamble FOR 2000;
            :: Postamble;
        }
    }
    
    STATE Postamble
    {
		SIP_G ? SIP_Signals_T.SIP_200_OK (?) : SIP_Signal 
        {
			Scenario_Gen_HTML_Verdict( HTML_File, 1, "PASS", "SIP_200_OK received after BYE");
			Print("SIP_ST_CC_TE_CR - VERDICT: PASS! - SIP_200_OK received");
			Print("SIP_ST_CC_TE_CR - ... ending the test scenario");
			
	        UDP_Data_G ! PH_SL.PH_Deact_Rq();
	        Scenario_Gen_HTML_Footer( HTML_File);
	        CloseFile( HTML_File);
	        STOP;
		}
		
		? .TPostamble()
        {
			Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "TPostamble timer expiered - SIP_200_OK not received after BYE");
			Print("SIP_ST_CC_TE_CR - VERDICT: FAIL! - TPostamble timer expiered");
			Print("SIP_ST_CC_TE_CR - ... ending the test scenario");
			UDP_Data_G ! PH_SL.PH_Deact_Rq();
			Scenario_Gen_HTML_Footer( HTML_File);
			CloseFile( HTML_File);
			STOP;
		}
    }
};